package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @GetMapping("/")
    public String index(Model model) {

        List<Map> result = BuildData();
        model.addAttribute("list", result);
        return "index/index";
    }

    @Autowired
    private TemplateEngine templateEngine;//模板引擎

    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() {
        //使用静态化技术生成页面  D:\temp\index.html

        //数据
        Context context = new Context();
        context.setVariable("list",BuildData());

        //输出流
        Writer writer = null;
        try {
            /*writer = new OutputStreamWriter(new FileOutputStream(
                    "T:\\A尚硅谷\\temp\\index.html"),"utf-8");*/
            writer = new PrintWriter("T:\\A尚硅谷\\temp\\index.html","utf-8");
            // =渲染=
            templateEngine.process("index/index",context,writer);//处理 == 渲染
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (writer != null)
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return Result.ok();
    }

    public List<Map> BuildData(){

        //三级分类视图集合
        List<BaseCategoryView> baseCategoryList =
                productFeignClient.getBaseCategoryList();

        Map<Long, List<BaseCategoryView>> baseCategoryListGroup = baseCategoryList
                .stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //最终结果 为集合[{},{},{}]
        List<Map> result = new ArrayList<>();

        // 行号
        int index = 1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1Entry :
                baseCategoryListGroup.entrySet()) {
//            category1Entry.getKey();      一级分类ID
//            category1Entry.getValue();    一级分类ID对应的集合
            Map category1Map = new HashMap();
            category1Map.put("index", index++);
            category1Map.put("categoryId", category1Entry.getKey());
            List<BaseCategoryView> category1List = category1Entry.getValue();
            category1Map.put("categoryName", category1List.get(0).getCategory1Name());

            //====二级分类集合==================================
            List<Map> category2ListMap = new ArrayList<>();

            Map<Long, List<BaseCategoryView>> category2Group = category1List
                    .stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            for (Map.Entry<Long, List<BaseCategoryView>> category2Entry : category2Group.entrySet()) {
                Map category2Map = new HashMap();
                category2Map.put("categoryId", category2Entry.getKey());
                List<BaseCategoryView> category2List = category2Entry.getValue();
                category2Map.put("categoryName", category2List.get(0).getCategory2Name());

                //====三级分类集合========================================
                List<Map> category3ListMap = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : category2List) {
                    Map category3Map = new HashMap();
                    category3Map.put("categoryId", baseCategoryView.getCategory3Id());
                    category3Map.put("categoryName", baseCategoryView.getCategory3Name());
                    category3ListMap.add(category3Map);
                }
                //=======================================================
                category2Map.put("categoryChild", category3ListMap);
                category2ListMap.add(category2Map);
            }
            //================================================
            category1Map.put("categoryChild", category2ListMap);
            result.add(category1Map);
        }
        return result;
    }

}
